﻿using DayuCode.Designer;
using DayuCode.Designer.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException += Application_ThreadException;
            ////Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AppInstance.StartupPath = Application.StartupPath;

            AppInstance.ApiServer = System.Configuration.ConfigurationManager.AppSettings["apiserver"];
            AppInstance.SourceCodeConfig = SrcCodeConfig.LoadConfig();
            UserConfig.Instance = UserConfig.LoadConfig();

            //string s = Newtonsoft.Json.JsonConvert.SerializeObject(new { id=122, Name="王华", admin=true, Paging=false});
            //System.Diagnostics.Debug.WriteLine(s);

            FormLogin loginForm = new FormLogin();
            loginForm.UserName = UserConfig.Instance.LoginId;
            loginForm.ShowDialog();
            bool isLogin = loginForm.IsLogin;
            loginForm.Dispose();

            if (isLogin)
            {
                UserConfig.Instance.LoginId = AppInstance.Author;
                Application.Run(new FormMain());
            }
            UserConfig.SaveConfig(UserConfig.Instance);
            SrcCodeConfig.SaveConfig(AppInstance.SourceCodeConfig);
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            if (ex == null) return;
            MessageBox.Show(string.Format("捕获到未处理异常：{0}\r\n异常信息：{1}", ex.GetType(), ex.Message));

            try
            {
                File.AppendAllText(Path.Combine(AppInstance.StartupPath, "logs.log"), string.Format("{0} 捕获到未处理异常：{1}\r\n异常信息：{2}\r\n异常堆栈：{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.GetType(), ex.Message, ex.StackTrace));
            }
            catch { }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            string msg = ex.Message;
            if(msg.Length > 1000)
            {
                msg = msg.Substring(0, 1000);
            }
            MessageBox.Show(string.Format("{0}", msg), "错误信息");

            try
            {
                File.AppendAllText(Path.Combine(AppInstance.StartupPath, "logs.log"), string.Format("{0} 捕获到未处理异常：{1}\r\n异常信息：{2}\r\n异常堆栈：{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.GetType(), ex.Message, ex.StackTrace));
            }
            catch { }
        }
    }
}
